<?php
$couponDiscount = '';
$getdiscountonclass = '';
$applycouponCode = '';
$coupon = '';
$subtotalAfterDiscount = '';
$dicountPrice = '';
$AllDataCart = Yii::app()->shoppingCart->getPositions();
$Allclasscourseid = '';
$Allbatchid = '';
$Allbatchdate = '';
$Allbatchtiming = '';
$Allbatchseat = '';
$description_class_batch = '';
$couponCode = (Yii::app()->user->hasState('appliedcoupon')) ? Yii::app()->user->getState('appliedcoupon') : '';

$username = '';
$useremail = '';
$loginuserId = getLoginUserId();
if (!empty($loginuserId)) {
    $loginuserDetail = Users::getFrontEndUser($loginuserId);
    $username = !empty($loginuserDetail->first_name) ? $loginuserDetail->first_name : $loginuserDetail->user->username;
    $useremail = Yii::app()->user->name;
    ;
} else {
    $loginuserId = '';
}

$coupon = Coupons::getCouponType($couponCode);
$subtotal = 0;
$afterDicountBatchPrice = '';
$cartCount = count($AllDataCart);
if ($cartCount > 0) {
    $i = 1;
    ?>
    <div class="table-responsive">
        <table class="table">
            <tr class="ttrrt">
                <th>serial no.</th>
                <th>workshop/course</th>
                <th>quantity</th>
                <th>discount</th>
                <th>total</th>
            </tr>
            <?php
            foreach ($AllDataCart as $AllDataCart) {
                $totalEarlyGroupDiscount = ClassBatch::totaldiscountonBatchinshoppingCart($AllDataCart->id, $AllDataCart->quantity);
                $getdiscountonclass = $totalEarlyGroupDiscount;
                if (!empty($coupon)) {
                    if ($coupon->apply_discount_to == 1) {
                        $applycouponCode = $couponCode;
                        $priceByCalss = $AllDataCart->batchprice;
                        $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                        $subtotal = $subtotal + $priceByCalss;
                        if ($cartCount == $i) {
                            $subtotalAfterDiscount = Coupons::getOrderofSubtotal($subtotal);
                            $dicountPrice = Coupons::getDiscountPrice($coupon);
                        }
                    }
                    if ($coupon->apply_discount_to == 2) {
                        $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                        $priceByCalss = $AllDataCart->batchprice;
                        $subtotal = $subtotal + $priceByCalss;
                        if ($cartCount == $i) {
                            $subtotalAfterDiscount = Coupons::totalofmatchingproducts($couponCode);
                            $dicountPrice = Coupons::getDiscountPrice($coupon);
                        }
                        $classArray = Coupons::allmatchingproducts($couponCode);
                        if (in_array($AllDataCart->classes_id, $classArray)) {
                            $applycouponCode = $couponCode;
                        } else {
                            $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                            $priceByCalss = $AllDataCart->batchprice;
                            $applycouponCode = '';
                        }
                    }
                    if ($coupon->apply_discount_to == 3) {
                        $classArray = Coupons::allmatchingproducts($couponCode);
                        if (in_array($AllDataCart->classes_id, $classArray)) {
                            $getdiscountonclass = ClassBatch::getDiscountOnClass($couponCode, $AllDataCart->price, $AllDataCart->quantity, $AllDataCart->id);
                            $subtotalAfterDiscount = $getdiscountonclass + $subtotalAfterDiscount;
                            $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                            $getdiscountonclass = $getdiscountonclass;
                            $priceByCalss = $getdiscountonclass;

                            $couponDiscountPrice = Coupons::getTotalDiscountPricecoupon($coupon, $withgroupearlydiscountprice);
                            $dicountPrice = $dicountPrice + $couponDiscountPrice;
                            $couponDiscount = $couponDiscountPrice;
                            $subtotal = $subtotal + $priceByCalss;
                            $applycouponCode = $couponCode;
                        } else {
                            $couponDiscountPrice = '';
                            $couponDiscount = '';
                            $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                            $getdiscountonclass = '';
                            $priceByCalss = $AllDataCart->batchprice;
                            $subtotalAfterDiscount = $priceByCalss + $subtotalAfterDiscount;
                            $subtotal = $subtotal + $priceByCalss;
                            $applycouponCode = '';
                        }
                    }
                    if ($coupon->apply_discount_to == 4) {
                        $chepeastBatchPrice = Coupons::cheapestmatchingproducts();
                        $getdiscountonclass = ClassBatch::getChepeastPrice($chepeastBatchPrice, $AllDataCart->quantity, $couponCode, $AllDataCart->classes_id, $AllDataCart->price, $AllDataCart->id);
                        if (!empty($getdiscountonclass)) {
                            $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                            $dicountPrice = Coupons::getTotalDiscountPricecoupon($coupon, $withgroupearlydiscountprice);
                            $couponDiscount = $dicountPrice;
                            $priceByCalss = $getdiscountonclass;
                            $subtotalAfterDiscount = $subtotalAfterDiscount + $getdiscountonclass;
                            $subtotal = $subtotal + $priceByCalss;
                            $applycouponCode = $couponCode;
                        } else {
                            $applycouponCode = '';
                            $couponDiscount = '';
                            $getdiscountonclass = '';
                            $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                            $priceByCalss = $AllDataCart->batchprice;
                            $subtotalAfterDiscount = $subtotalAfterDiscount + $priceByCalss;
                            $subtotal = $subtotal + $priceByCalss;
                        }
                    }

                    if ($coupon->apply_discount_to == 5) {
                        $expensiveBatchPrice = Coupons::mostexpensivematchingproducts();
                        $getdiscountonclass = ClassBatch::getChepeastPrice($expensiveBatchPrice, $AllDataCart->quantity, $couponCode, $AllDataCart->classes_id, $AllDataCart->price, $AllDataCart->id);
                        if (!empty($getdiscountonclass)) {
                            $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                            $priceByCalss = $getdiscountonclass;
                            $dicountPrice = Coupons::getTotalDiscountPricecoupon($coupon, $withgroupearlydiscountprice);
                            $couponDiscount = $dicountPrice;
                            $subtotalAfterDiscount = $subtotalAfterDiscount + $getdiscountonclass;
                            $subtotal = $subtotal + $AllDataCart->batchprice;
                            $applycouponCode = $couponCode;
                        } else {
                            $getdiscountonclass = '';
                            $applycouponCode = '';
                            $couponDiscount = '';
                            $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                            $priceByCalss = $AllDataCart->batchprice;
                            $subtotalAfterDiscount = $subtotalAfterDiscount + $priceByCalss;
                            $subtotal = $subtotal + $priceByCalss;
                        }
                    }
                } else {
                    $applycouponCode = '';
                    $priceByCalss = $AllDataCart->batchprice;
                    $withgroupearlydiscountprice = $AllDataCart->batchprice + $totalEarlyGroupDiscount;
                    $subtotalAfterDiscount = $subtotalAfterDiscount;
                    $subtotal = $subtotal + $priceByCalss;
                }
                if (isset($AllDataCart->classes_id)) {
                    $classes = Classes::model()->findByPk($AllDataCart->classes_id);
                    $classcoursetitle = $classes->class_titile;
                    $classcourseid = $classes->id;
                    $viewUrl = '/workshop-'.$classcourseid.'/'.$classes->class_seo_title;
                    $classcoursetype = 'classes';
                }
                $imagePath = ($classes->users_id == '') ? 'backend/' . $classes->backend_user_id : $classes->users_id;
                $imageName = (ClassImage::getPrimaryImage($classcourseid) != '') ? getRequestbaseUrl() . getuploadFolderDir() . SkillConfig::getClassCourseImagePath($imagePath) . 'thumbs/' . ClassImage::getPrimaryImage($classcourseid) : getWorkNoshopimage(SkillConfig::skill_shopping_image);
                $imageName = '<img  src="' . $imageName . '" alt="' . $classcoursetitle . '" />';
                ?>
                <tr> <td><?php echo $i; ?></td>
                    <td>
                        <div class="item-wrap">
                            <span class="pull-left"> <?php echo CHtml::link($imageName, array($viewUrl)); ?></span>
                            <div class="close-img" onclick="return removeClassBatch(<?php echo $AllDataCart->id; ?>);"></div>
                            <div class="shopping-itemd pull-right">
                                <h4><?php echo substr($classcoursetitle, 0, 10) . '..'; ?></h4>
                                <?php if (!empty($applycouponCode)) { ?>
                                    <small>Apply Coupon <br/>                             
                                        coupon code:<?php echo $applycouponCode; ?></small> 
                                <?php } ?>

                            </div>
                        </div>
                    </td>
                    <td><input type="text" class="field" value="<?php echo $AllDataCart->quantity; ?>" onchange="return QuantityUpdate(this.value,<?php echo $AllDataCart->id; ?>, '<?php echo $classcoursetype; ?>', 'errorSeats<?php echo $AllDataCart->id; ?>', '<?php echo $AllDataCart->batchtiming; ?>', '<?php echo $AllDataCart->startdate; ?>', '<?php echo $AllDataCart->class_venue_id; ?>', '<?php echo $AllDataCart->classes_id; ?>');">
                        <p id="errorSeats<?php echo $AllDataCart->id; ?>" class="errorMessage"></p></td>
                    <td>
                        <?php
                        ClassBatch::getpricebySeatsinshoppingCart($AllDataCart->id, $AllDataCart->quantity);
                        '<br>';
                        echo (!empty($couponDiscount)) ? '<span class="discount">COUPON DISCOUNT Rs. ' . $couponDiscount . '<span>' : '';
                        ?>
                    </td>
                    <td>
                        <?php echo (!empty($getdiscountonclass)) ? '<del>' . number_format($withgroupearlydiscountprice, 2) . '</del><br>' . number_format($priceByCalss, 2) : number_format($withgroupearlydiscountprice, 2); ?>

                    </td>
                </tr>
                <?php
                $i++;
            }
            ?>


        </table>
    </div>  
    <div class="total">
        <div class="row ">
            <div class="col-lg-6 col-md-6 col-sm-6">
                <div class="col-lg-4 pad-rl">
                    <h3>Apply coupon code:</h3></div>
                <div class="col-lg-4 col-md-4 col-sm-4 mopo"><input type="text" class="discountField form-control book-control" id="discount_id" name="discount_id" placeholder="Enter Discount Code" ></div>
                <div class="col-lg-4 col-md-4 col-sm-4 mopo"><input type="button"  value="Apply" onclick="return checkcouponcode();" style="padding: 8px;margin: 10px;">
                </div>
                <div class="errorMessage" id="discount_error" style="clear:both;text-align: center"></div>
            </div>
            <div class="col-lg-6 col-md-6 col-sm-6">
                <?php if (!empty($dicountPrice)) { ?>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                        <div class="total-charge"><h5>total price:</h5>
                            <h5>coupon Discount:</h5>
                            <h5>after discount price:</h5></div> 
                    </div>


                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                        <div class="total-charge"><h5  class=" rupee"><?php echo '  ' . number_format($subtotal, 2); ?></h5>
                            <h5 class=""><?php echo '  ' . $dicountPrice; ?></h5>
                            <h5 class=" rupee"><?php echo '  ' . number_format($subtotalAfterDiscount, 2); ?></h5></div> 
                    </div>
                <?php } else { ?>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                        <div class="total-charge"><h5>Sub Total:</h5>
                        </div> 
                    </div>
                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
                        <div class="total-charge"> <h5 class=" rupee"> <?php echo '  ' . number_format($subtotal, 2); ?></h5></div> 
                    </div>

                <?php } ?>
            </div>

        </div><!-- row ends -->
        <div>
            <div class="continue">
                <?php
                Yii::app()->session['totalamountcart'] = (!empty($dicountPrice)) ? $subtotalAfterDiscount : $subtotal;
                Yii::app()->session['shopping_cart_session'] = 1;
                $this->renderPartial('//shoppingCart/_form_EBS', array('subtotal' => Yii::app()->session['totalamountcart'], 'loginuserId' => $loginuserId, 'useremail' => $useremail, 'username' => $username));
                ?>
            </div><!--secondrow ends--></div>
    </div>

<?php } else { ?>
    <div class="empty-wrap">
        <p>There are no items to display in your cart</p>
        <div class=" empty-button pull-right">

        </div>

    </div>
    <?php
    $loginuserid = getLoginUserId();
    Coupons::unsetSessionShopingcart();
    if (!empty($loginuserid)) {
        ShoppingCartByUser::model()->deleteAll('user_id=' . getLoginUserId());
    }
}
?>
